home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / misc1 / iv26_w30.zip / SOURCES / SENSOR.C < prev    next >
C/C++ Source or Header  |  1992-02-19  |  7KB  |  308 lines

  1. /*
  2.  * Copyright (c) 1987, 1988, 1989 Stanford University
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and its
  5.  * documentation for any purpose is hereby granted without fee, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Stanford not be used in advertising or
  9.  * publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  Stanford makes no representations about
  11.  * the suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  16.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  20.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. /*
  24.  * Implementation of input event handling.
  25.  */
  26.  
  27. #include <InterViews/interactor.h>
  28. #include <InterViews/sensor.h>
  29. #include <InterViews/world.h>
  30. #include <string.h>
  31. #include <time.h>
  32.  
  33. Sensor* allEvents;
  34. Sensor* onoffEvents;
  35. Sensor* updownEvents;
  36. Sensor* noEvents;
  37. Sensor* stdsensor;
  38.  
  39. Sensor::Sensor () {
  40.     register int i;
  41.  
  42.     mask = initmask;
  43.     timer = false;
  44.     for (i = 0; i < 8; i++) {
  45.     down[i] = 0;
  46.     up[i] = 0;
  47.     }
  48.     Reference();
  49. }
  50.  
  51. Sensor::Sensor (register Sensor* s) {
  52.     register int i;
  53.  
  54.     mask = s->mask;
  55.     timer = s->timer;
  56.     sec = s->sec;
  57.     usec = s->usec;
  58.     clkticks = s->clkticks;
  59.     for (i = 0; i < 8; i++) {
  60.     down[i] = s->down[i];
  61.     up[i] = s->up[i];
  62.     }
  63.     Reference();
  64. }
  65.  
  66. Sensor::~Sensor () {
  67.     /* nothing to do */
  68. }
  69.  
  70. void InitSensors () {
  71.     allEvents = new Sensor;
  72.     allEvents->Catch(MotionEvent);
  73.     allEvents->Catch(DownEvent);
  74.     allEvents->Catch(UpEvent);
  75.     allEvents->Catch(KeyEvent);
  76.     allEvents->Catch(EnterEvent);
  77.     allEvents->Catch(LeaveEvent);
  78.     onoffEvents = new Sensor;
  79.     onoffEvents->Catch(EnterEvent);
  80.     onoffEvents->Catch(LeaveEvent);
  81.     updownEvents = new Sensor;
  82.     updownEvents->Catch(UpEvent);
  83.     updownEvents->Catch(DownEvent);
  84.     noEvents = new Sensor;
  85.     stdsensor = noEvents;
  86. }
  87.  
  88. void Sensor::Catch (EventType t) {
  89.     register int i;
  90.  
  91.     switch (t) {
  92.     case MotionEvent:
  93.         mask |= motionmask;
  94.         break;
  95.     case DownEvent:
  96.         mask |= downmask;
  97.         SetMouseButtons(down);
  98.         break;
  99.     case UpEvent:
  100.         mask |= upmask;
  101.         SetMouseButtons(up);
  102.         break;
  103.     case KeyEvent:
  104.         mask |= keymask;
  105.         down[0] |= 0xfffffff8;
  106.         for (i = 1; i < 8; i++) {
  107.         down[i] = 0xffffffff;
  108.         }
  109.         break;
  110.     case EnterEvent:
  111.         mask |= entermask;
  112.         break;
  113.     case LeaveEvent:
  114.         mask |= leavemask;
  115.         break;
  116.     case FocusInEvent:
  117.     case FocusOutEvent:
  118.         mask |= focusmask;
  119.         break;
  120.     case ChannelEvent:
  121.         /* ignore */
  122.         break;
  123.     case TimerEvent:
  124.         timer = true;
  125.         sec = 0;
  126.         usec = 0;
  127.         break;
  128.     }
  129. }
  130.  
  131. void Sensor::CatchButton (EventType t, int b) {
  132.     switch (t) {
  133.     case DownEvent:
  134.         mask |= downmask;
  135.         SetButton(down, b);
  136.         break;
  137.     case UpEvent:
  138.         mask |= upmask;
  139.         SetButton(up, b);
  140.         break;
  141.     case KeyEvent:
  142.         mask |= keymask;
  143.         SetButton(down, b);
  144.         break;
  145.     default:
  146.         /* ignore */
  147.         break;
  148.     }
  149. }
  150.  
  151. void Sensor::CatchChannel (int ch) {
  152.     channels |= (1 << ch);
  153.     if (ch > maxchannel) {
  154.     maxchannel = ch+1;
  155.     }
  156. }
  157.  
  158. void Sensor::CatchTimer (int s, int u) {
  159.     timer = true;
  160.     sec = s;
  161.     usec = u;
  162.     clkticks = CLOCKS_PER_SEC*s + (CLOCKS_PER_SEC*u)/1000;
  163. }
  164.  
  165. void Sensor::Ignore (EventType t) {
  166.     register int i;
  167.  
  168.     switch (t) {
  169.     case MotionEvent:
  170.         mask &= ~motionmask;
  171.         break;
  172.     case DownEvent:
  173.         ClearMouseButtons(down);
  174.         if (!MouseButtons(up)) {
  175.         mask &= ~downmask;
  176.         }
  177.         break;
  178.     case UpEvent:
  179.         ClearMouseButtons(up);
  180.         if (!MouseButtons(down)) {
  181.         mask &= ~upmask;
  182.         }
  183.         break;
  184.     case KeyEvent:
  185.         down[0] &= ~0xfffffff8;
  186.         for (i = 1; i < 8; i++) {
  187.         down[i] = 0;
  188.         }
  189.         mask &= ~keymask;
  190.         break;
  191.     case EnterEvent:
  192.         mask &= ~entermask;
  193.         break;
  194.     case LeaveEvent:
  195.         mask &= ~leavemask;
  196.         break;
  197.     case FocusInEvent:
  198.     case FocusOutEvent:
  199.         mask &= ~focusmask;
  200.         break;
  201.     case ChannelEvent:
  202.         channels = 0;
  203.         maxchannel = 0;
  204.         break;
  205.     case TimerEvent:
  206.         timer = false;
  207.         break;
  208.     }
  209. }
  210.  
  211. void Sensor::IgnoreButton (EventType t, int b) {
  212.     register int i;
  213.  
  214.     switch (t) {
  215.     case DownEvent:
  216.         ClearButton(down, b);
  217.         if (!MouseButtons(down) && !MouseButtons(up)) {
  218.         mask &= ~downmask;
  219.         }
  220.         break;
  221.     case UpEvent:
  222.         ClearButton(up, b);
  223.         if (!MouseButtons(up) && !MouseButtons(down)) {
  224.         mask &= ~upmask;
  225.         }
  226.         break;
  227.     case KeyEvent:
  228.         ClearButton(down, b);
  229.         if ((down[0] & 0xfffffff8) == 0) {
  230.         mask &= ~keymask;
  231.         for (i = 1; i < 8; i++) {
  232.             if (down[i] != 0) {
  233.             mask |= keymask;
  234.             break;
  235.             }
  236.         }
  237.         }
  238.         break;
  239.     default:
  240.         /* ignore */
  241.         break;
  242.     }
  243. }
  244.  
  245. void Sensor::IgnoreChannel (int ch) {
  246. /*    channels &= ~(1 << ch);
  247.     if (channels == 0) {
  248.     maxchannel = 0;
  249.     } else {
  250.     while ((channels & (1 << maxchannel)) == 0) {
  251.         --maxchannel;
  252.     }
  253.     }
  254. */
  255. }
  256.  
  257. void Sensor::CatchRemote () {
  258.     mask |= substructmask;
  259. }
  260.  
  261. void Sensor::IgnoreRemote () {
  262.     mask &= ~substructmask;
  263. }
  264.  
  265. Event& Event::operator= (register Event& e) {
  266.     target = e.target;
  267.     timestamp = e.timestamp;
  268.     eventType = e.eventType;
  269.     x = e.x;
  270.     y = e.y;
  271.     control = e.control;
  272.     meta = e.meta;
  273.     shift = e.shift;
  274.     shiftlock = e.shiftlock;
  275.     leftmouse = e.leftmouse;
  276.     middlemouse = e.middlemouse;
  277.     rightmouse = e.rightmouse;
  278.     button = e.button;
  279.     len = e.len;
  280.     if (e.keystring == e.keydata) {
  281.     keystring = keydata;
  282.     strncpy(keydata, e.keydata, sizeof(keydata));
  283.     } else {
  284.     keystring = e.keystring;
  285.     }
  286.     channel = e.channel;
  287.     w = e.w;
  288.     wx = e.wx;
  289.     wy = e.wy;
  290.     return *this;
  291. }
  292.  
  293. void Event::GetAbsolute (Coord& absx, Coord& absy) {
  294.     if (w == nil) {
  295.     register Interactor* r;
  296.  
  297.     for (r = target; r->Parent() != nil; r = r->Parent());
  298.     w = (World*)r;
  299.     }
  300.     absx = w->InvMapX(wx);
  301.     absy = w->InvMapY(wy);
  302. }
  303.  
  304. void Event::GetAbsolute (World*& s, Coord& absx, Coord& absy) {
  305.     GetAbsolute(absx, absy);
  306.     s = w;
  307. }
  308.